package com.winning.sjzc.common.controller;

import com.winning.sjzc.common.service.CommonApiService;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * description:公共处理类
 *
 * @author yanxf@winning.com.cn
 * @date 2019/08/13 14:34
 */
@RequestMapping("/commonapi")
@RestController
public class CommonApiController {

  @Autowired
  private CommonApiService commonApiService;

  @RequestMapping(value = "/showFile", method = RequestMethod.GET)
  public void showFile(HttpServletRequest request, HttpServletResponse response) {
    String colName = request.getParameter("busiCol");
    byte[] fileByte = commonApiService.getFile(colName);
    OutputStream toClient = null;
    try {
      // 重置response
      response.reset();
      response.setContentType("bin");
      toClient = new BufferedOutputStream(response.getOutputStream());
      response.setContentType("application/octet-stream");
      response.addHeader("Content-Disposition", "attachment;filename=ii.png");
      response.addHeader("Content-Length", "" + fileByte.length);
      toClient.write(fileByte);
      toClient.flush();
      toClient.close();
    } catch (Exception e) {
      if (toClient != null) {
        try {
          toClient.flush();
          toClient.close();
        } catch (Exception ee) {
        }
      }
    }
  }
}
